package com.data.service.catalog;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.data.dao.domain.DataCatalog;
import com.data.dao.service.DataCatalogService;
import com.data.model.catalog.DimensionCatalogDTO;
import com.data.model.catalog.IndexCatalogDTO;
import com.data.model.catalog.TableCatalogDTO;
import com.data.model.enums.CatalogTypeEnum;
import com.data.model.constant.DataCatalogConstant;
import com.data.model.request.create.CreateDimensionRequest;
import com.data.model.request.create.CreateIndexRequest;
import com.data.model.request.create.CreateTableRequest;
import com.data.model.request.query.QueryDimensionRequest;
import com.data.model.request.query.QueryIndexRequest;
import com.data.model.request.query.QueryTableRequest;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class CatalogServiceImpl implements CatalogService {
    @Resource
    private DataCatalogService dataCatalogService;

    @Override
    public void saveTable(CreateTableRequest request) {
        TableCatalogDTO tableCatalogDTO = request.getTableCatalogDTO();

        DataCatalog dataCatalog = new DataCatalog();
        dataCatalog.setMainType(tableCatalogDTO.getMainType());
        dataCatalog.setSubType(tableCatalogDTO.getSubType());
        dataCatalog.setCode(tableCatalogDTO.getCode());
        dataCatalog.setName(tableCatalogDTO.getName());
        dataCatalog.setDescription(tableCatalogDTO.getDesc());

        dataCatalog.setBizType(request.getBizType());
        dataCatalog.setCreator(request.getCreator());
        dataCatalog.setGmtCreate(new Date());
        dataCatalog.setGmtModified(new Date());

        Map<String, String> attributes = new HashMap<>();
        attributes.put(DataCatalogConstant.STRUCT, JSONObject.toJSONString(tableCatalogDTO));
        dataCatalog.setAttributes(JSONObject.toJSONString(attributes));
        dataCatalogService.save(dataCatalog);
    }

    @Override
    public List<TableCatalogDTO> queryTable(QueryTableRequest request) {
        List<DataCatalog> catalogs = dataCatalogService.getBaseMapper().selectList(Wrappers.<DataCatalog>lambdaQuery()
                .eq(DataCatalog::getCode, request.getCode())
                .eq(DataCatalog::getMainType, CatalogTypeEnum.TABLE.getCode())
        );

        if (CollectionUtils.isEmpty(catalogs)) {
            return Lists.newArrayList();
        }
        List<TableCatalogDTO> tableCatalogs = new ArrayList<>();
        catalogs.forEach(catalog -> {
            Map<String, String> attributes = JSON.parseObject( catalog.getAttributes().toString(), new TypeReference<Map<String, String>>(){});
            TableCatalogDTO tableCatalogDTO = JSONObject.parseObject(attributes.get(DataCatalogConstant.STRUCT), TableCatalogDTO.class);

            tableCatalogs.add(tableCatalogDTO);
        });

        return tableCatalogs;
    }

    @Override
    public void saveDimension(CreateDimensionRequest request) {
        DimensionCatalogDTO dimensionCatalogDTO = request.getDimensionCatalogDTO();

        DataCatalog dataCatalog = new DataCatalog();
        dataCatalog.setMainType(dimensionCatalogDTO.getMainType());
        dataCatalog.setSubType(dimensionCatalogDTO.getSubType());
        dataCatalog.setCode(dimensionCatalogDTO.getCode());
        dataCatalog.setName(dimensionCatalogDTO.getName());
        dataCatalog.setDescription(dimensionCatalogDTO.getDesc());

        dataCatalog.setBizType(request.getBizType());
        dataCatalog.setCreator(request.getCreator());
        dataCatalog.setGmtCreate(new Date());
        dataCatalog.setGmtModified(new Date());

        Map<String, String> attributes = new HashMap<>();
        attributes.put(DataCatalogConstant.STRUCT, JSONObject.toJSONString(dimensionCatalogDTO));

        dataCatalogService.save(dataCatalog);
    }

    @Override
    public List<DimensionCatalogDTO> queryDimension(QueryDimensionRequest request) {
        return null;
    }

    @Override
    public void saveIndex(CreateIndexRequest request) {
        IndexCatalogDTO indexCatalogDTO = request.getIndexCatalogDTO();

        DataCatalog dataCatalog = new DataCatalog();
        dataCatalog.setMainType(indexCatalogDTO.getMainType());
        dataCatalog.setSubType(indexCatalogDTO.getSubType());
        dataCatalog.setCode(indexCatalogDTO.getCode());
        dataCatalog.setName(indexCatalogDTO.getName());
        dataCatalog.setDescription(indexCatalogDTO.getDesc());

        dataCatalog.setBizType(request.getBizType());
        dataCatalog.setCreator(request.getCreator());
        dataCatalog.setGmtCreate(new Date());
        dataCatalog.setGmtModified(new Date());

        Map<String, String> attributes = new HashMap<>();
        attributes.put(DataCatalogConstant.STRUCT, JSONObject.toJSONString(indexCatalogDTO));

        dataCatalogService.save(dataCatalog);
    }

    @Override
    public List<IndexCatalogDTO> queryIndex(QueryIndexRequest request) {
        return null;
    }
}
